﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.Data;

namespace Jumpton.SFA.Server.BusinessRule
{
    public class UserDeviceBoundManager : IBusinessLogic
    {
        #region IBusinessLogic Members

        object IBusinessLogic.Load(decimal ID)
        {
            return ObjectRow.Load(typeof(MobileDeviceInfo), "id='" + ID + "'");
        }

        object IBusinessLogic.LoadByCode(string Code)
        {
            throw new NotImplementedException();
        }

        object IBusinessLogic.LoadWithCondition(string Condition)
        {
            return new ObjectTable(typeof(UserDeviceBound), Condition);
        }

        void IBusinessLogic.Save(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion

        public UserDeviceBound LoadByID(int id)
        {
            return (UserDeviceBound)((IBusinessLogic)this).Load(id);
        }
        public ObjectTable LoadWithCondition(String condition)
        {
            return (ObjectTable)((IBusinessLogic)this).LoadWithCondition(condition);
        }

        public static UserDeviceBound GetUserDeviceBound(decimal UserId)
        {
            return (UserDeviceBound)ObjectRow.Load(typeof(UserDeviceBound), "UserId=" + UserId.ToString());
        }

        public static UserDeviceInfo GetUserDeviceInfo(decimal UserId)
        {
            return (UserDeviceInfo)ObjectRow.Load(typeof(UserDeviceInfo), "A.UserId=" + UserId.ToString());
        }
        public static UserDeviceBound GetUserDeviceByDeviceID(decimal deviceId)
        {
            return (UserDeviceBound)ObjectRow.Load(typeof(UserDeviceBound), string.Format(" DeviceId={0}", deviceId));
        }

        public Boolean Delete(Decimal id)
        {
            String sql = "delete from UserDeviceBound where id=" + id.ToString();
            SqlData.ExecuteScalar(sql);

            return true;
        }



        #region chenhailong
        /// <summary>
        /// 新增设备
        /// </summary>
        /// <returns></returns>
        private static bool InsertMobileDeviceInfo(string imeiCode, int doMainId, string userId)
        {
            bool flag = false;
            if (!string.IsNullOrEmpty(imeiCode))
            {
                try
                {
                    string date = BusinessLogic.GetDateTimeString(DateTime.Now);
                    string sSql = string.Format(@"insert into MobileDeviceInfo (IMEICode,ACTIVE,DOMAIN_ID,
                                    LAST_UPDATER,LAST_UPDATE_TIME,CREATE_TIME) values ('{0}',{1},{2},'{3}',{4},{4});select @@indentity;",
                                           imeiCode, 1, doMainId, userId, date);
                    int mdi_id = (int)SqlData.ExecuteSql(sSql);
                    sSql = string.Format(@"insert into UserDeviceBound(UserID,DeviceID,ACTIVE,DOMAIN_ID,
                                LAST_UPDATER, LAST_UPDATE_TIME,CREATE_TIME)
                                values ({0},{1},{2},{3},{0},{4},{4})", userId, mdi_id, 1, doMainId, date);
                    SqlData.ExecuteSql(sSql);
                    flag = true;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            return flag;
        }

        /// <summary>
        /// 检测用户和设备是否绑定
        /// </summary>
        /// <returns>-1 失效 -2插入失败</returns>
        public static int CheckUserDevice(string imeiCode, int doMainId, string userNo)
        {
            return Convert.ToInt32(SqlData.ExecuteScalarSP("SP_INSERT_MOBILEDEVICE", imeiCode, userNo, doMainId));
        }


        /// <summary>
        /// 用户绑定设备
        /// </summary>
        /// <param name="udb"></param>
        /// <returns></returns>
        private bool SaveUserDeviceBound(UserDeviceBound udb)
        {
            bool flag = false;
            if (udb != null)
            {
                try
                {
                    udb.Save();
                    flag = true;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            return flag;
        }


        /// <summary>
        /// 检测设备是否存在
        /// </summary>
        /// <param name="dev"></param>
        /// <returns>存在返回true,反之返回false</returns>
        private static bool CheckMobileDeviceInfo(string mobileInfo)
        {
            string sSql = string.Format(@"select count(*) from MobileDeviceInfo where IMEICode = '{0}'", mobileInfo);
            int k = (int)SqlData.ExecuteSql(sSql);
            if (k > 0)
            {
                return true;
            }
            return false;
        }

        #endregion

    }
}
